package com.dlb.andorid.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.bean.AddressRetrieval;
import com.dlb.andorid.bean.MyAddress;
import com.dlb.andorid.bean.Province;
import com.dlb.andorid.presenter.AddressPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonRecycleActivity;

import java.util.ArrayList;
import java.util.List;

public class AddressSearchActivity extends BaseCommonRecycleActivity implements XContract.IAddressView {


	AddressPresenter mPresenter;
	BaseQuickAdapter<AddressRetrieval, BaseViewHolder> mAddressRetrievalAdapter;

	long province_id;
	long city_id;
	long area_id;

	private EditText et_search;
	private View tv_search;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		province_id = (long) getIntentData("province_id");
		city_id = (long) getIntentData("city_id");
		area_id = (long) getIntentData("area_id");
	}

	@Override
	protected int getTopViewId() {
		return R.layout.activity_address_search;
	}

	@Override
	protected void initViews() {
		super.initViews();
		et_search = findViewById(R.id.et_search);
		tv_search = findViewById(R.id.tv_search);

		tv_search.setOnClickListener(this);
		refreshLayout.setEnableLoadMore(false);
		refreshLayout.setEnableRefresh(false);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == tv_search) {

			String str = et_search.getText().toString();

			mPresenter.getAddressRetrieval(province_id, city_id, area_id, str);
		}
	}

	@Override
	protected void initData() {
		super.initData();
		mPresenter.getAddressRetrieval(province_id, city_id, area_id, "");
	}

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new AddressPresenter(this);
		return mPresenter;
	}

	@Override
	protected RecyclerView.ItemDecoration getItemDecoration() {
		return null;
	}

	List<AddressRetrieval> mAddressRetrievals = new ArrayList<>();

	@Override
	protected BaseQuickAdapter getAdapter() {

		mAddressRetrievalAdapter = new BaseQuickAdapter<AddressRetrieval, BaseViewHolder>(R.layout.list_item_string, mAddressRetrievals) {
			@Override
			protected void convert(BaseViewHolder helper, AddressRetrieval item) {
				helper.setText(R.id.tv_name, item.getName());
			}
		};

		mAddressRetrievalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Intent intent = new Intent();
				intent.putExtra("data",mAddressRetrievals.get(position));
				setResult(RESULT_OK,intent);
				finish();
			}
		});

		return mAddressRetrievalAdapter;
	}

	@Override
	protected RecyclerView.LayoutManager getLayoutManager() {
		return new LinearLayoutManager(this);
	}

	@Override
	protected String getTitleTxt() {
		return "搜索地址";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void addAddressSuccess(String msg) {

	}

	@Override
	public void getAddressCitySuccess(List<Province> data) {

	}

	@Override
	public void getAddressSuccess(List<MyAddress> data) {

	}

	@Override
	public void getAddressRetrievalSuccess(List<AddressRetrieval> data) {
		mAddressRetrievals.clear();
		mAddressRetrievals.addAll(data);
		mAddressRetrievalAdapter.notifyDataSetChanged();
	}

	@Override
	public void deleteAddSuccess(XServerMsg data) {

	}

}
